package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.activity.HiddendangerUnSubmit;
import com.example.safeassistan.home.adapter.TaskCheckPersonListAdapter;
import com.example.safeassistan.home.adapter.TaskCheckTypeListAdapter;
import com.example.safeassistan.home.adapter.UserListAdapter;
import com.example.safeassistan.home.model.TaskCheckTypeModel;
import com.example.safeassistan.home.model.TaskPersonListModel;
import com.example.safeassistan.home.model.UserListModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityUserList extends BaseActivity implements View.OnClickListener {
    private UserListAdapter adapter;

    private TaskCheckPersonListAdapter adapter1;
    private List<UserListModel.ResultDTO> data;
    private LinearLayout llNodata;
    private LinearLayout llTop;
    private SmartRefreshLayout refresh;
    private RecyclerView rcView;
    private TextView _barTvRight;

    private String userId="";
    private String userName="";



    private String positionId="";
    private String departId="";


    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityUserList.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id._barTvRight:
                //完成
                Intent intent = new Intent();
                intent.putExtra("id",userId);
                intent.putExtra("name",userName);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.riskdetail_checklog_fragment;
    }

    @Override
    protected void initView() {
        setTitleText("人员选择");
        positionId=getIntent().getStringExtra("positionId");
        departId=getIntent().getStringExtra("departId");
        llNodata=findViewById(R.id.llNodata);
        llTop=findViewById(R.id.llTop);
        _barTvRight=findViewById(R.id._barTvRight);
        llTop.setVisibility(View.GONE);
        refresh=findViewById(R.id.refresh);
        rcView=findViewById(R.id.rcView);
        _barTvRight.setText("完成");
        _barTvRight.setVisibility(View.VISIBLE);
        _barTvRight.setOnClickListener(this::onClick);
        if (positionId!=null&&departId!=null&&!positionId.equals("")){
            getData1();
        }else {
            getData();
        }

    }


    //获取数据
    public void getData1(){
        Map jsonObject=new HashMap<>();
        jsonObject.put("departId",departId);
        jsonObject.put("positionId",positionId);
        new HttpRequest(ActivityUserList.this).doGet(UrlConstants.taskgetAllPerson, null, jsonObject,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), TaskPersonListModel.class);
                        if (obj instanceof TaskPersonListModel) {
                            TaskPersonListModel model=(TaskPersonListModel) obj;
                            if (model.getResult().size() > 0) {
                                llNodata.setVisibility(View.GONE);
                                rcView.setVisibility(View.VISIBLE);
                                adapter1=new TaskCheckPersonListAdapter(model.getResult());
                                rcView.setLayoutManager(new LinearLayoutManager(ActivityUserList.this));
                                rcView.setAdapter(adapter1);
                                adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        if (model.getResult().get(position).getSelect()){
                                            model.getResult().get(position).setSelect(false);
                                            userId=userId.replace(model.getResult().get(position).getId()+",","");
                                            userName=userName.replace(model.getResult().get(position).getRealname()+",","");
                                        }else {
                                            model.getResult().get(position).setSelect(true);
                                            userId=userId+model.getResult().get(position).getId()+",";
                                            userName=userName+model.getResult().get(position).getRealname()+",";
                                        }
                                        adapter.notifyDataSetChanged();
                                    }
                                });
                            } else {
                                llNodata.setVisibility(View.VISIBLE);
                                refresh.finishRefreshWithNoMoreData();
                            }
                        }
                    }else {
                        Toast.makeText(ActivityUserList.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }

    //获取数据
    public void getData(){

        new HttpRequest(ActivityUserList.this).doGet(UrlConstants.taskgetAllUser, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), UserListModel.class);
                        if (obj instanceof UserListModel) {
                            UserListModel model=(UserListModel) obj;
                                if (model.getResult().size() > 0) {
                                    llNodata.setVisibility(View.GONE);
                                    rcView.setVisibility(View.VISIBLE);
                                    data=model.getResult();
                                    adapter=new UserListAdapter(data);
                                    rcView.setLayoutManager(new LinearLayoutManager(ActivityUserList.this));
                                    rcView.setAdapter(adapter);
                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                                            if (data.get(position).getSelect()){
                                                data.get(position).setSelect(false);
                                                userId=userId.replace(data.get(position).getId()+",","");
                                                userName=userId.replace(data.get(position).getRealname()+",","");
                                            }else {
                                                data.get(position).setSelect(true);
                                                userId=userId+data.get(position).getId()+",";
                                                userName=userName+data.get(position).getRealname()+",";
                                            }

                                            adapter.notifyDataSetChanged();
                                        }
                                    });
                                } else {
                                    llNodata.setVisibility(View.VISIBLE);
                                    refresh.finishRefreshWithNoMoreData();
                                }
                        }
                    }else {
                        Toast.makeText(ActivityUserList.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}